using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Update.Controls
{
    /// <summary>
    /// 自定义深色主题TextBox，带有窄滚动条
    /// </summary>
    public class DarkTextBox : RichTextBox
    {
        public DarkTextBox()
        {
            // 设置控件样式
            SetStyle(ControlStyles.OptimizedDoubleBuffer | 
                     ControlStyles.ResizeRedraw | 
                     ControlStyles.AllPaintingInWmPaint, true);

            // 初始化控件属性
            BackColor = Color.FromArgb(45, 45, 48);
            ForeColor = Color.White;
            BorderStyle = BorderStyle.None;
            Font = new Font("微软雅黑", 9F);
            WordWrap = false;
            ScrollBars = RichTextBoxScrollBars.Vertical;
            Margin = new Padding(0);
            Padding = new Padding(5);
            ReadOnly = true;
            
            // 附加自定义滚动条
            this.HandleCreated += (s, e) => InitScrollBar();
        }

        private void InitScrollBar()
        {
            try
            {
                // 应用视觉样式
                Application.EnableVisualStyles();
                
                // 设置滚动条颜色
                if (Environment.OSVersion.Version.Major >= 10)
                {
                    // Windows 10 及以上版本使用现代滚动条
                    SetWindowTheme(Handle, "DarkMode_Explorer", null);
                }
                else
                {
                    // 旧版 Windows 使用标准滚动条
                    SetWindowTheme(Handle, "Explorer", null);
                }
            }
            catch
            {
                // 忽略任何异常
            }
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            
            // 防止闪烁
            if (m.Msg == 0x0014) // WM_ERASEBKGND
            {
                m.Result = IntPtr.Zero;
            }
        }

        [System.Runtime.InteropServices.DllImport("uxtheme.dll", CharSet = System.Runtime.InteropServices.CharSet.Unicode)]
        private static extern int SetWindowTheme(IntPtr hWnd, string pszSubAppName, string pszSubIdList);
    }
} 